import unittest
import datetime
from qproc import QueryProcessorTest
import errors
import main
import sys

import pytz
SOFIA_TZINFO = pytz.timezone('Europe/Sofia')

class QueryProcessorFake1(QueryProcessorTest):
    """Class which generates constant output"""

    def __init__(self, now=None):
        if now is None:
            now = datetime.datetime.now(SOFIA_TZINFO).replace(tzinfo=None)        
        stops = {
            533:{'name':u'name533', 'description':u'description533', 'services':['A94', 'A280']},
            534:{'name':u'name534', 'description':u'description534', 'services':['A94', 'A280']},
            742:{'name':u'name742', 'description':u'description742', 'services':['A94', 'A280']},
            1697:{'name':u'name1697', 'description':u'description1697', 'services':['A9', 'A280', 'A306']},
            1700:{'name':u'name1700', 'description':u'description1700', 'services':['A84', 'A94', 'A284']},
            1568:{'name':u'name1568', 'description':u'description1568', 'services':['A67']}
            }
        vehicles = {
            (534, 'A94'): [10, 18, 30, 45, 65],
            (534, 'A280'): [12, 20, 34, 44, 70],
            (1700, 'A94'): [3, 18, 22, 23, 40],
            (1700, 'A84'): [5, 10, 15, 20]
        }
        QueryProcessorTest.__init__(self, now, stops, vehicles)

    def get_vehicles(self, stop_number, service):
        """Get vehicle scheduled times for service service at stop stop_number"""
        if (stop_number, service) in self.vehicles:
            return QueryProcessorTest.get_vehicles(self, stop_number, service)
        else:
            return []

class ResponsePageValuesTest(unittest.TestCase):
    def now_plus(self, minutes):
        return self.now + datetime.timedelta(minutes=minutes)
    
    def setUp(self):
        self.now = datetime.datetime(2009, 7, 1, 12, 0)
        self.qp = QueryProcessorFake1(self.now)

    def test_return_results(self):
        self.assertEquals(main.get_response_page_values(self.qp, [534], ['94'], None, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '0534', 'error': None, 'desc': u'description534', 'name': u'name534', 'name_lat': u'name534',
            'vehicles': [('A94', self.now_plus(10), 10), ('A94', self.now_plus(18), 18), ('A94', self.now_plus(30), 30), ('A94', self.now_plus(45), 45), ('A94', self.now_plus(65), 65)]}]})
            
        self.assertEquals(main.get_response_page_values(self.qp, [534], ['a94'], 10, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '0534', 'error': None, 'desc': u'description534', 'name': u'name534', 'name_lat': u'name534',
            'vehicles': [('A94', self.now_plus(10), 10)]}]})

        self.assertEquals(main.get_response_page_values(self.qp, [534], [], 19, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '0534', 'error': None, 'desc': u'description534', 'name': u'name534', 'name_lat': u'name534',
            'vehicles': [('A94', self.now_plus(10), 10), ('A280', self.now_plus(12), 12), ('A94', self.now_plus(18), 18)]}]})
            
        self.assertEquals(main.get_response_page_values(self.qp, [1700, 534], ['a28', '84'], 30, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '1700', 'error': None, 'desc': u'description1700', 'name': u'name1700', 'name_lat': u'name1700',
            'vehicles': [('A84', self.now_plus(5), 5), ('A84', self.now_plus(10), 10), ('A84', self.now_plus(15), 15), ('A84', self.now_plus(20), 20)]},
            {'stop': '0534', 'error': None, 'desc': u'description534', 'name': u'name534', 'name_lat': u'name534',
            'vehicles': [('A280', self.now_plus(12), 12), ('A280', self.now_plus(20), 20)]}]})
        
    def test_return_errors(self):
        self.assertEquals(main.get_response_page_values(self.qp, [1700], ['A280'], 60, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '1700', 'error': 'No matching lines for [A280] on stop 1700', 'desc': u'description1700', 'name': u'name1700', 'name_lat': u'name1700',
            'vehicles': []}]})
        
        self.assertEquals(main.get_response_page_values(self.qp, [534], ['280'], 10, self.now, False),
            {'now': self.now, 'stops': [
            {'stop': '0534', 'error': 'No vehicles are due in the next 10 minutes', 'desc': u'description534', 'name': u'name534', 'name_lat': u'name534',
            'vehicles': []}]})       

        self.assertEquals(main.get_response_page_values(self.qp, [666], ['66'], 60, self.now, False),
            {'now': self.now, 'stops': [{'stop': '0666', 'error': 'Unexisting stop (666)', 'desc': None, 'name': None, 'name_lat': None}]})

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(ResponsePageValuesTest)
    unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)